package com.tianhui.consignortwofei.mvp.model.enty.ocrResult.vehicleLicense;

import com.baidu.ocr.sdk.exception.OCRError;
import com.baidu.ocr.sdk.model.Word;
import com.baidu.ocr.sdk.utils.Parser;

import org.json.JSONException;
import org.json.JSONObject;

/**
 * @author fgs
 * @date 2020/5/19
 * @description
 **/
public class VehicleLicenseResultParser implements Parser<VehicleLicenseResult> {

    public VehicleLicenseResultParser() {
    }

    public VehicleLicenseResult parse(String json) throws OCRError {
        OCRError error;
        try {
            JSONObject jsonObject = new JSONObject(json);
            if (jsonObject.has("error_code")) {
                error = new OCRError(jsonObject.optInt("error_code"), jsonObject.optString("error_msg"));
                error.setLogId(jsonObject.optLong("log_id"));
                throw error;
            } else {
                VehicleLicenseResult result = new VehicleLicenseResult();
                JSONObject wordResult = jsonObject.optJSONObject("words_result");
                if (wordResult != null) {
                    result.setBrandModel(this.map(wordResult.optJSONObject("品牌型号")));
                    result.setIssueDate(this.map(wordResult.optJSONObject("发证日期")));
                    result.setUseNature(this.map(wordResult.optJSONObject("使用性质")));
                    result.setEngineNumber(this.map(wordResult.optJSONObject("发动机号码")));
                    result.setPlatNumber(this.map(wordResult.optJSONObject("号牌号码")));
                    result.setAddress(this.map(wordResult.optJSONObject("住址")));
                    result.setSeal(this.map(wordResult.optJSONObject("发证单位")));
                    result.setOwner(this.map(wordResult.optJSONObject("所有人")));
                    result.setRegisterDate(this.map(wordResult.optJSONObject("注册日期")));
                    result.setIdentificationNumber(this.map(wordResult.optJSONObject("车辆识别代号")));
                    result.setVehicleType(this.map(wordResult.optJSONObject("车辆类型")));

                    result.setTotalWeight(this.map(wordResult.optJSONObject("总质量")));
                    result.setTractionMass(this.map(wordResult.optJSONObject("准牵引总质量")));
                    result.setApprovedLoadCapacity(this.map(wordResult.optJSONObject("核定载质量")));
                    result.setCurbWeight(this.map(wordResult.optJSONObject("整备质量")));
                    result.setOverallSizes(this.map(wordResult.optJSONObject("外廓尺寸")));
                    result.setRecordDate(this.map(wordResult.optJSONObject("检验记录")));
                    result.setIndate(this.map(wordResult.optJSONObject("备注")));
                }
                return result;
            }

        } catch (JSONException var5) {
            error = new OCRError(283505, "Server illegal response " + json, var5);
            throw error;
        }
    }

    private Word map(JSONObject jsonObject) {
        Word word = null;
        if (jsonObject != null) {
            word = new Word();
            word.setWords(jsonObject.optString("words"));
        }

        return word;
    }
}
